
var nodeCheckedSilent = false;
function nodeChecked (event, node){
    if(nodeCheckedSilent){
        return;
    }
    nodeCheckedSilent = true;
    checkAllParent(node);
    checkAllSon(node);
    nodeCheckedSilent = false;
}

var nodeUncheckedSilent = false;
function nodeUnchecked  (event, node){
    if(nodeUncheckedSilent)
        return;
    nodeUncheckedSilent = true;
    uncheckAllParent(node);
    uncheckAllSon(node);
    nodeUncheckedSilent = false;
}

//选中全部父节点
function checkAllParent(node){
    $('#grant').treeview('checkNode',node.nodeId,{silent:true});
    var parentNode = $('#grant').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)){
        return;
    }else{
        checkAllParent(parentNode);
    }
}
//取消全部父节点
function uncheckAllParent(node){
    $('#grant').treeview('uncheckNode',node.nodeId,{silent:true});
    var siblings = $('#grant').treeview('getSiblings', node.nodeId);
    var parentNode = $('#grant').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)) {
        return;
    }
    var isAllUnchecked = true;  //是否全部没选中
    for(var i in siblings){
        if(siblings[i].state.checked){
            isAllUnchecked=false;
            break;
        }
    }
    if(isAllUnchecked){
        uncheckAllParent(parentNode);
    }

}

//级联选中所有子节点
function checkAllSon(node){
    $('#grant').treeview('checkNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            checkAllSon(node.nodes[i]);
        }
    }
}
//级联取消所有子节点
function uncheckAllSon(node){
    $('#grant').treeview('uncheckNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            uncheckAllSon(node.nodes[i]);
        }
    }
}
$(function () {
    $("#grant").dialog({
        title:"角色授权",
        btnName:"授权",
        btnBind:function(){
            var rows=$("#table").bootstrapTable("getSelections");
            var param="rid="+rows[0].roleCode;
            var arr=$("#grant").treeview("getChecked");
            console.info(arr)
            for(var i=0;i<arr.length;i++){
                param=param+"&mids="+arr[i].id;
            }
            console.info(param)
            $.post("sys/role/grant",param,function (msg) {
                $("#grant").dialog("close");
                toastr.warning("信息提示",msg);
            },"text")
        }
    });

  $("#UpRole").dialog({
      title:"修改角色",
      btnName:"修改",
      btnBind:function () {
          var params=$("#updateRole").serializeArray();
          var param="";
          for(var i in params){
              if(i==params.length-1){
                  param+=params[i].name+"="+params[i].value;
              }else{
                  param+=params[i].name+"="+params[i].value+"&";
              }
          }
          console.info(param);

          $.post("sys/role/updateRole",param,function (msg) {
              $("#UpRole").dialog("close");
              $("#table").bootstrapTable("refresh");
              toastr.warning("信息提示",msg);
          },"text")

      }
  })



    $("#AddRole").dialog({
        title:"新增角色",
        btnName:"新增",
        btnBind:function () {
            var params=$("#adRole").serializeArray();
            var param="";
            for(var i in params){
                if(i==params.length-1){
                    param+=params[i].name+"="+params[i].value;
                }else{
                    param+=params[i].name+"="+params[i].value+"&";
                }
            }
            console.info(param);

            $.post("sys/role/addRole",param,function (msg) {
                $("#AddRole").dialog("close");
                $("#table").bootstrapTable("refresh");
                toastr.warning("信息提示",msg);
            },"text")

        }
    })



})

/**
 * 授权
 */

function prepareGrant() {
    var rows=$("#table").bootstrapTable("getSelections");
    if(rows.length==0){
        swal("请选择一项授权！");
        return;
    }
    var param="rid="+rows[0].roleCode;
    $.getJSON("sys/role/tree",param,function(result){
        $("#grant").treeview({
            data:result,
            multiSelect:true,
            showCheckbox:true,
            onNodeChecked:nodeChecked ,
            onNodeUnchecked:nodeUnchecked
        })
        $("#grant").dialog("open");
    })

}


function FormatState(val,row,index) {
    if(val==0){
        return '可用';
    }else if(val==1){
        return '禁用';
    }else{
        return '未标明';
    }
}
function FormatOperation(value,row,index) {
    var operation = "<button class='btn btn-info' onclick='updateRole("+JSON.stringify(row)+")'><i class='glyphicon glyphicon-edit'></i> 修 改</button><button class='btn btn-danger'onclick='deleteRole("+row.roleId+")'><i class='glyphicon glyphicon-remove'></i> 删 除</button>";
    return operation;
}
$("td,th").addClass("text-center");

function search4Role() {
    var param=$("#condition").serializeArray();
    console.info(param)

    var obj={};
    for(var i in param){
        obj[param[i].name]=param[i].value;
    }
    $("#table").bootstrapTable("refresh",{query:obj});
}

function updateRole(row) {
    console.info(row)
    $("#updateRole input[name='roleCode']").val(row.roleCode);
    $("#updateRole input[name='roleId']").val(row.roleId);
    $("#updateRole input[name='roleName']").val(row.roleName);
    $("#updateRole textarea[name='roleDesc']").val(row.roleDesc);
    $("#updateRole select[name='deleteFlag']").val(row.deleteFlag);

    $(".modal .modal-body #UpRole").attr("class","show");
    $("#UpRole").dialog("open");
}

function deleteRole(rid) {
    var param="rid="+rid;
    $.post("sys/role/delRole",param,function (msg) {
        toastr.warning(msg);
        $("#table").bootstrapTable("refresh")
    },"text")
}

function addRole() {
    $(".modal .modal-body #AddRole").attr("class","show");
    $("#AddRole").dialog("open");
}